package summer.presum;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution525 {
    // https://leetcode.cn/problems/contiguous-array/solutions/809920/gong-shui-san-xie-qian-zhui-he-ha-xi-bia-q400
    public int findMaxLength(int[] nums) {
        int n = nums.length;
        int[] sum = new int[n + 1];
        for (int i = 0; i < n; i++) {
            sum[i + 1] = sum[i] + nums[i];
        }
        int max = 0;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = (i + 1) % 2; j < i; j += 2) {
                if ((sum[i + 1] - sum[j]) << 1 == i - j + 1) {
                    max = Math.max(max, i - j + 1);
                    break;
                }
            }
            if (max > i) break;
        }
        return max;
    }
}
